package com.shop.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.constant.MessageConstant;
import com.shop.entity.QueryPageBean;
import com.shop.entity.Result;
import com.shop.pojo.GoodsKind;
import com.shop.service.GoodsKindService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/goods_kind")
@Api(tags = "商品分类管理")
public class GoodsKindController {
    @Reference
    private GoodsKindService goodsKindService;

//    @PreAuthorize("hasAuthority('ADD_GOODSKIND')")
    @PostMapping("/add")
    @ApiOperation("添加商品分类接口")
    public Result add(@RequestBody GoodsKind goodsKind){
        if (goodsKindService.findGoodsKindName(goodsKind.getName())!=null){
            return new Result(false,MessageConstant.ADD_GOODSKIND_FAIL);
        }else{
            goodsKindService.add(goodsKind);
            return new Result(true,MessageConstant.ADD_GOODSKIND_SUCCESS);
        }
    }

    @PreAuthorize("hasAuthority('QUERY_GOODSKIND')")
    @PostMapping("/pageQuery")
    @ApiOperation("商品分类分页接口")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        Page<GoodsKind> page=goodsKindService.pageQuery(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        return new Result(true,MessageConstant.QUERY_GOODSKIND_SUCCESS,page);
    }

//    @PreAuthorize("hasAuthority('DELETE_GOODSKIND')")
    @DeleteMapping("/delete")
    @ApiOperation("删除商品分类接口")
    @ApiImplicitParams({@ApiImplicitParam(dataType = "int",name = "id",value = "商品分类id",required = true,example = "0")})
    public Result delete(Integer id){
        try {
            goodsKindService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_GOODSKIND_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_GOODSKIND_SUCCESS);
    }

//    @PreAuthorize("hasAuthority('EDIT_GOODSKIND')")
    @PutMapping("/edit")
    @ApiOperation("修改商品分类接口")
    public Result edit(@RequestBody GoodsKind goodsKind){
//        try {
//            goodsKindService.edit(goodsKind);
//        }catch (Exception e){
//            e.printStackTrace();
//            return new Result(false,MessageConstant.EDIT_GOODSKIND_FAIL);
//        }
//        return new Result(true,MessageConstant.EDIT_GOODSKIND_SUCCESS);
        GoodsKind g = goodsKindService.findById(goodsKind.getId());
        if (goodsKindService.findGoodsKindName(goodsKind.getName())!=null && !g.getName().equals(goodsKind.getName())){
            return new Result(false,MessageConstant.EDIT_GOODSKIND_FAIL);
        }else{
            goodsKindService.edit(goodsKind);
            return new Result(true,MessageConstant.EDIT_GOODSKIND_SUCCESS);
        }
    }

    @GetMapping("/findById")
    @ApiOperation("根据商品分类id查找商品分类接口")
    public Result findById(@RequestParam(required = true) Integer id){
        try {
            GoodsKind goodsKind=goodsKindService.findById(id);
            return new Result(true,MessageConstant.QUERY_GOODSKIND_SUCCESS,goodsKind);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_GOODSKIND_FAIL);
        }
    }

    @GetMapping("/findAll")
    @ApiOperation("显示商品分类下拉列表接口")
    public Result findAll(){
        try {
            List<GoodsKind> list=goodsKindService.findAll();
            return new Result(true,MessageConstant.QUERY_GOODSKIND_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_GOODSKIND_FAIL);
        }
    }

}
